@extends("admin.frame_layout")
<?php $top_selected = "信息管理";$page_menu = "文章列表" ?>
@section('content')
    <div class="formbody">
        <div class="formtitle"><span>文章编辑</span></div>

        <form action="{{route('admin.news.save',['id'=>isset($news)?($news->id):(0)])}}"
              method="post" id="news"  enctype="multipart/form-data">
            {!! csrf_field() !!}

            <ul class="forminfo">

                {!! inputText("文章标题","title","text",
                    isset($news)?$news->title:"",
                    "标题建议最长不超过50个字符！") !!}

                <li>
                    <label class="lable">文章分类</label>
                    <select name="cid" id="cid">
                        <option value="">请必须选择文章分类</option>
                        @foreach($classifies as $i)
                            <option value="{{$i->id}}" {{isset($news)?($news->cid==$i->id?"selected":""):""}}>{{$i->title}}</option>
                        @endforeach
                    </select>
                    <i for="cate"></i>
                </li>

                <li>
                    <label class="lable">封面图</label>
                    @if ($news->img)
                        <img src="{{asset($news->img)}}" alt="" style="width:150px;height:150px;">
                        <input type="file" name="file" id="file" value="{{$news->img}}">
                    @else
                        <input type="file" name="file" id="file">
                    @endif
                </li>

                <li>
                    <label class="lable">文章内容</label>
                    {{-- 加载编辑器的容器 --}}
                    <div style="float: left;width: 800px;">
                        <script name="content" id="content"
                                type="text/plain">{!! $news->content or "" !!}</script>
                    </div>
                    <div style="clear: both;"></div>
                </li>
                <li><label class="lable">&nbsp;</label>
                    <input type="submit" class="btn" value="确认保存"/>
                </li>
            </ul>
        </form>
    </div>
@endsection

@section('footer')
    <script src="{{asset('admin/js/ueditor/ueditor.config.js')}}"></script>
    <script src="{{asset('admin/js/ueditor/ueditor.all.js')}}"></script>
    {{-- 实例化编辑器 --}}
    <script type="text/javascript">
        var ue = UE.getEditor('content', {
            initialFrameHeight: 250
        });

        $("#news").validate({
            rules: {
                cid: "required",
                title: {
                    required: true,
                    minlength: 1,
                    maxlength: 100
                }
            },
            errorPlacement: function (error, element) {
                $(element).closest("form").find("i[for='" + element.attr("id") + "']").html(error);
            },
            submitHandler: function (form) {
                if (ue.hasContents()) {
                    ajaxSub(form);
                } else {
                    toastr["error"]("请填写文章详情！");
                }
            }
        });

        /**
         * ajax提交请求
         */
        function ajaxSub(form) {
            $(form).ajaxSubmit({
                dataType: "json",
                type: "post",
                success: function showSuccess(data) {
                    if (data.code == 0) {
                        toastr["success"](data.msg);
                        setInterval(function () {
                            window.location = "{{route('admin.news')}}";
                        }, 2000);
                    } else {
                        toastr["error"](data.msg);
                    }
                }
            });
        }
    </script>
@endsection
